package com.linkage.usercenter.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.base.RequestInfo;
import com.linkage.usercenter.dao.*;
import com.linkage.usercenter.pojo.entity.*;
import com.linkage.usercenter.pojo.vo.*;
import com.linkage.usercenter.service.IInspectUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InspectUserServiceImpl implements IInspectUserService {

    private static final Logger logger = LoggerFactory.getLogger(UserOperationServiceImpl.class);

    @Autowired
    private CorePersonInfoMapper personDao;

    @Autowired
    private InspectEnterpriseMapper enterpriseDao;

    @Autowired
    private InspectSecurityInfoMapper inspectInfoDao;
    @Autowired
    private InspectSecurityMapper inspectDao;

    @Autowired
    private UserSubInfoMapper userSubInfoDao;
    //获取保安用户信息
    @Override
    public void getInspectUserInfo(RequestInfo<InspectUserInfoQUERY> query, InspectUserInfoVO vo) {
        String personCode = query.getBuss().getUserCode();
        if(StrUtil.isAllBlank(personCode, query.getBuss().getUserMobile(),query.getBuss().getPersonCardNumber())){
            throw new ValidateException("参数信息校验不通过，保安代码、身份证号码和手机号码不能全为空!");
        }
        CorePersonInfo personInfo = null;


        if (StrUtil.isNotBlank(personCode)) {
            personInfo = personDao.getRecordByPersonCardNumber(query.getBuss().getPersonCardNumber());
        }else if (StrUtil.isNotBlank(personCode)) {
            personInfo = personDao.getRecordByPersonCodeAndMobile(personCode, null);
        }else{
            personInfo = personDao.getRecordByPersonCodeAndMobile(null, query.getBuss().getUserMobile());
        }
        if (ObjectUtil.isNull(personInfo)) {
            throw new ValidateException("查询无此保安人员信息!");
        }
        //数据转换
        UserInfoVO userInfoVO = new UserInfoVO();
        this.coreDataToUserInfoVO(userInfoVO, personInfo);
        InspectSecurity securityDO = inspectDao.getRecordByPersonCode(personInfo.getPersonCode());
        InspectSecurityVO securityVO = new InspectSecurityVO();
        this.dataToInspectSecurityVO(securityDO, securityVO);
        List<InspectSecurityInfo> securityInfoDOList = inspectInfoDao.queryRecordsByPersonCode(personInfo.getPersonCode());
        List<InspectSecurityInfoVO> inspectInfoVOList = new ArrayList<InspectSecurityInfoVO>();
        this.dataListToInspectSecurityInfoVOList(securityInfoDOList, inspectInfoVOList);
        vo.setUserInfoVO(userInfoVO);
        vo.setSecurityVO(securityVO);
        vo.setInspectInfoVOList(inspectInfoVOList);

    }

    //提交保安核查信息
    @Override
    public void submitInspectInfo(RequestInfo<InspectSecurityInfoVO> query) {
        if(query.getBuss() == null || StrUtil.hasBlank(query.getBuss().getUserId(),query.getBuss().getInspectUserid())){
            throw new ValidateException("参数信息校验不通过，请检查!");
        }
        InspectSecurityInfoVO requestInfo = query.getBuss();
        InspectSecurityInfo securityInfoDO = new InspectSecurityInfo();
        securityInfoDO.setId(IdUtil.simpleUUID());
        securityInfoDO.setInspectUserid(requestInfo.getInspectUserid());
        securityInfoDO.setAbarbeitungAgency(requestInfo.getAbarbeitungAgency());
        securityInfoDO.setAbarbeitungDescription(requestInfo.getAbarbeitungDescription());
        securityInfoDO.setAbarbeitungReason(requestInfo.getAbarbeitungReason());
        securityInfoDO.setAbarbeitungTime(getDateByString(requestInfo.getAbarbeitungTime()));
        securityInfoDO.setUserId(requestInfo.getUserId());
        if(securityInfoDO.getAbarbeitungTime() == null){
            securityInfoDO.setAbarbeitungTime(new Date());
        }
        inspectInfoDao.insert(securityInfoDO);
    }

    //提交企业核查信息
    @Override
    public void submitInspectEnter(RequestInfo<InspectEnterpriseVO> query) {
        if(query.getBuss() == null || StrUtil.hasBlank(query.getBuss().getInspectedCompany(),
                query.getBuss().getInspectUserid())){
            throw new ValidateException("参数信息校验不通过，请检查!");
        }
        InspectEnterpriseVO requestInfo = query.getBuss();
        InspectEnterprise enterpriseDO = new InspectEnterprise();
        enterpriseDO.setCard(requestInfo.getCard());
        enterpriseDO.setClothing(requestInfo.getClothing());
        enterpriseDO.setCompanyAddress(requestInfo.getCompanyAddress());
        enterpriseDO.setContract(requestInfo.getContract());
        enterpriseDO.setCorrection(requestInfo.getCorrection());
        enterpriseDO.setId(IdUtil.simpleUUID());
        enterpriseDO.setInspectAgency(requestInfo.getInspectAgency());
        enterpriseDO.setInspectAgencyCode(requestInfo.getInspectAgencyCode());
        enterpriseDO.setInspectedCompany(requestInfo.getInspectedCompany());
        enterpriseDO.setInspectedCode(requestInfo.getInspectedCode());
        enterpriseDO.setInspectTime(getDateByString(requestInfo.getInspectTime()));
        enterpriseDO.setInspectUserid(requestInfo.getInspectUserid());
        enterpriseDO.setInstitution(requestInfo.getInstitution());
        enterpriseDO.setInspectedCompanySuggestion(requestInfo.getInspectedCompanySuggestion());
        enterpriseDO.setGunInstitution(requestInfo.getGunInstitution());
        enterpriseDO.setEquipment(requestInfo.getEquipment());
        enterpriseDO.setMatter(requestInfo.getMatter());
        enterpriseDO.setOtherscontent(requestInfo.getOtherscontent());
        enterpriseDO.setPermission(requestInfo.getPermission());
        enterpriseDO.setSaveVideo(requestInfo.getSaveVideo());
        enterpriseDO.setSignature(requestInfo.getSignature());
        enterpriseDO.setSuggestion(requestInfo.getSuggestion());
        if(enterpriseDO.getInspectTime() == null){
            enterpriseDO.setInspectTime(new Date());
        }
        enterpriseDao.insert(enterpriseDO);

    }

    //补充保安用户信息
    @Override
    public void supplementUserInfo(RequestInfo<InspectSecurityQUERY> query) {
        InspectSecurityQUERY requestInfo = query.getBuss();
        if(requestInfo == null || StrUtil.isBlank(requestInfo.getUserId())){
            throw new ValidateException("参数信息校验不通过，用户代码不能为空!");
        }

        String personCode = requestInfo.getUserId();
        CorePersonInfo personInfo = personDao.getRecordByPersonCodeAndMobile(personCode, null);
        if (ObjectUtil.isNull(personInfo)) {
            throw new ValidateException("查询无此保安人员信息!");
        }
        InspectSecurity securityDO = inspectDao.getRecordByPersonCode(personInfo.getPersonCode());
        if(securityDO!=null){
            if(StrUtil.isNotBlank(requestInfo.getExclusiveSkill())){
                securityDO.setExclusiveSkill(requestInfo.getExclusiveSkill());
            }
            if(StrUtil.isNotBlank(requestInfo.getFamilyName())){
                securityDO.setFamilyName(requestInfo.getFamilyName());
            }
            if(StrUtil.isNotBlank(requestInfo.getFamilyRelation())){
                securityDO.setFamilyRelation(requestInfo.getFamilyRelation());
            }
            if(StrUtil.isNotBlank(requestInfo.getFirstWork())){
                securityDO.setFirstWork(requestInfo.getFirstWork());
            }
            if(StrUtil.isNotBlank(requestInfo.getGunCardAgency())){
                securityDO.setGunCardAgency(requestInfo.getGunCardAgency());
            }
            if(StrUtil.isNotBlank(requestInfo.getGunCardExpire())){
                securityDO.setGunCardExpire(getDateByString(requestInfo.getGunCardExpire()));
            }
            if(StrUtil.isNotBlank(requestInfo.getGunCardTime())){
                securityDO.setGunCardTime(getDateByString(requestInfo.getGunCardTime()));
            }
            if(StrUtil.isNotBlank(requestInfo.getGunNo())){
                securityDO.setGunNo(requestInfo.getGunNo());
            }
            if(StrUtil.isNotBlank(requestInfo.getGunType())){
                securityDO.setGunType(requestInfo.getGunType());
            }
            if(StrUtil.isNotBlank(requestInfo.getIdCardBeginExpire())){
                securityDO.setIdCardBeginExpire(getDateByString(requestInfo.getIdCardBeginExpire()));
            }
            if(StrUtil.isNotBlank(requestInfo.getIdCardEndExpire())){
                securityDO.setIdCardEndExpire(getDateByString(requestInfo.getIdCardEndExpire()));
            }
            if(StrUtil.isNotBlank(requestInfo.getMilitaryStatus())){
                securityDO.setMilitaryStatus(requestInfo.getMilitaryStatus());
            }
            if(StrUtil.isNotBlank(requestInfo.getPhotoImgUrl())){
                securityDO.setPhotoImgUrl(requestInfo.getPhotoImgUrl());
            }
            if(StrUtil.isNotBlank(requestInfo.getPoliticsIdentity())){
                securityDO.setPoliticsIdentity(requestInfo.getPoliticsIdentity());
            }
            if(StrUtil.isNotBlank(requestInfo.getSecurityCardAgency())){
                securityDO.setSecurityCardAgency(requestInfo.getSecurityCardAgency());
            }
            if(StrUtil.isNotBlank(requestInfo.getSecurityCardTime())){
                securityDO.setSecurityCardTime(getDateByString(requestInfo.getSecurityCardTime()));
            }
            if(StrUtil.isNotBlank(requestInfo.getSecurityjobTime())){
                securityDO.setSecurityjobTime(getDateByString(requestInfo.getSecurityjobTime()));
            }
            if(StrUtil.isNotBlank(requestInfo.getSpeciality())){
                securityDO.setSpeciality(requestInfo.getSpeciality());
            }
            if(StrUtil.isNotBlank(requestInfo.getSecurityLevel())){
                securityDO.setSecurityLevel(requestInfo.getSecurityLevel());
            }
            inspectDao.updateByPrimaryKey(securityDO);
        }else{
            securityDO = new InspectSecurity();
            securityDO.setId(IdUtil.simpleUUID());
            securityDO.setUserId(personCode);

            securityDO.setExclusiveSkill(requestInfo.getExclusiveSkill());
            securityDO.setFamilyName(requestInfo.getFamilyName());
            securityDO.setFamilyRelation(requestInfo.getFamilyRelation());
            securityDO.setFirstWork(requestInfo.getFirstWork());
            securityDO.setGunCardAgency(requestInfo.getGunCardAgency());
            securityDO.setGunCardExpire(getDateByString(requestInfo.getGunCardExpire()));
            securityDO.setGunCardTime(getDateByString(requestInfo.getGunCardTime()));
            securityDO.setGunNo(requestInfo.getGunNo());
            securityDO.setGunType(requestInfo.getGunType());
            securityDO.setIdCardBeginExpire(getDateByString(requestInfo.getIdCardBeginExpire()));
            securityDO.setIdCardEndExpire(getDateByString(requestInfo.getIdCardEndExpire()));
            securityDO.setMilitaryStatus(requestInfo.getMilitaryStatus());
            securityDO.setPhotoImgUrl(requestInfo.getPhotoImgUrl());
            securityDO.setPoliticsIdentity(requestInfo.getPoliticsIdentity());
            securityDO.setSecurityCardAgency(requestInfo.getSecurityCardAgency());
            securityDO.setSecurityCardTime(getDateByString(requestInfo.getSecurityCardTime()));
            securityDO.setSecurityjobTime(getDateByString(requestInfo.getSecurityjobTime()));
            securityDO.setSpeciality(requestInfo.getSpeciality());
            securityDO.setSecurityLevel(requestInfo.getSecurityLevel());
            inspectDao.insert(securityDO);
        }
        if(StrUtil.isBlank(securityDO.getPhotoImgUrl())){
            return;
        }
        UserSubInfo userSubInfoDO = userSubInfoDao.selectByPrimaryKey(securityDO.getUserId());
        if(userSubInfoDO != null ){
            userSubInfoDO.setUserPhotoUrl(securityDO.getPhotoImgUrl());
            userSubInfoDao.updateByPrimaryKey(userSubInfoDO);
        }else {
            userSubInfoDO = new UserSubInfo();
            userSubInfoDO.setUserCode(securityDO.getUserId());
            userSubInfoDO.setUserPhotoUrl(securityDO.getPhotoImgUrl());
            userSubInfoDao.insert(userSubInfoDO);
        }

    }

    private void dataListToInspectSecurityInfoVOList(List<InspectSecurityInfo> securityInfoDOList, List<InspectSecurityInfoVO> inspectInfoVOList) {
        if(securityInfoDOList == null || securityInfoDOList.isEmpty()){
            return;
        }
        for (InspectSecurityInfo infoDO : securityInfoDOList) {
            InspectSecurityInfoVO infoVO = new InspectSecurityInfoVO();
            infoVO.setAbarbeitungAgency(infoDO.getAbarbeitungAgency());
            infoVO.setAbarbeitungDescription(infoDO.getAbarbeitungDescription());
            infoVO.setAbarbeitungReason(infoDO.getAbarbeitungReason());
            infoVO.setAbarbeitungTime(getDateString(infoDO.getAbarbeitungTime()));
            infoVO.setInspectUserid(infoDO.getInspectUserid());
            infoVO.setUserId(infoDO.getUserId());
            inspectInfoVOList.add(infoVO);
        }
    }

    private void dataToInspectSecurityVO(InspectSecurity securityDO, InspectSecurityVO securityVO) {
        if(securityDO == null){
            return;
        }
        securityVO.setExclusiveSkill(securityDO.getExclusiveSkill());
        securityVO.setFamilyName(securityDO.getFamilyName());
        securityVO.setFamilyRelation(securityDO.getFamilyRelation());
        securityVO.setFirstWork(securityDO.getFirstWork());
        securityVO.setGunCardAgency(securityDO.getGunCardAgency());
        securityVO.setGunCardExpire(getDateString(securityDO.getGunCardExpire()));
        securityVO.setGunCardTime(getDateString(securityDO.getGunCardTime()));
        securityVO.setGunNo(securityDO.getGunNo());
        securityVO.setGunType(securityDO.getGunType());
        securityVO.setIdCardBeginExpire(getDateString(securityDO.getIdCardBeginExpire()));
        securityVO.setIdCardEndExpire(getDateString(securityDO.getIdCardEndExpire()));
        securityVO.setMilitaryStatus(securityDO.getMilitaryStatus());
        securityVO.setPhotoImgUrl(securityDO.getPhotoImgUrl());
        securityVO.setPoliticsIdentity(securityDO.getPoliticsIdentity());
        securityVO.setSecurityCardAgency(securityDO.getSecurityCardAgency());
        securityVO.setSecurityCardTime(getDateString(securityDO.getSecurityCardTime()));
        securityVO.setSecurityjobTime(getDateString(securityDO.getSecurityjobTime()));
        securityVO.setSpeciality(securityDO.getSpeciality());
        securityVO.setSecurityLevel(securityDO.getSecurityLevel());
    }

    private static String getDateString(Date date){
        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
    }
    private static Date getDateByString(String dateString){
        if(StrUtil.isBlank(dateString)){
            return null;
        }
        return DateUtil.parse(dateString, "yyyy-MM-dd HH:mm:ss");
    }


    private void coreDataToUserInfoVO(UserInfoVO bussVO,  CorePersonInfo personInfo) {
        bussVO.setUserCode(personInfo.getPersonCode());
        bussVO.setUserCode(personInfo.getPersonCode());
        bussVO.setUserRealName(personInfo.getPersonRealName());
        bussVO.setUserDisplayName(personInfo.getPersonRealName());
        bussVO.setUserMobile(personInfo.getPersonMobile());
        bussVO.setPersonCardType(personInfo.getPersonCardType());
        bussVO.setPersonCardNumber(personInfo.getPersonCardNumber());
        bussVO.setPersonSex(personInfo.getPersonSex());
        bussVO.setPersonBirthDate(personInfo.getPersonBirthDate());
        bussVO.setPersonClass(personInfo.getPersonClass());
        bussVO.setPersonBirthAddress(personInfo.getPersonBirthAddress());
        bussVO.setPersonLiveAddress(personInfo.getPersonLiveAddress());
        bussVO.setPersonJobAddress(personInfo.getPersonJobAddress());
        bussVO.setPersonJobCardType(personInfo.getPersonJobCardType());
        bussVO.setPersonJobCardNumber(personInfo.getPersonJobCardNumber());
        bussVO.setPersondptName(personInfo.getPersonCompanyCode());
    }


}
